<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.model' );
jimport( 'joomla.utilities.date' );
jimport( 'joomla.filesystem.file' );
jimport( 'joomla.filesystem.folder' );
jimport( 'joomla.access.rule' );


class jzmluvyModelUpravapartner extends Jmodel
{
  var $_data = null;
  var $_id = null;
  
	function __construct(){
	 
    $this->params	= &JComponentHelper::getParams('com_jzmluvy');
    $id = JRequest::getVar( 'id', '', '', 'array' );
		$this->setId($id[0]);
    $this->nastavenie = & jzmluvyHelper::nastavenie();
		
		parent::__construct();
	}

	function setId($id)
	{
		// Set event id and wipe data
		$this->_id	    = $id;
		$this->_data	= null;
	}

	function &getData()
	{

		if ($this->_loadData())
		{
		}
		else  $this->_initData();
  	return $this->_data;
	}

	function _loadData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = 'SELECT *'
					. ' FROM #__jzmluvy_partner'
					. ' WHERE id = '.$this->_id
					;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();
			return (boolean) $this->_data;
		}
		return true;
	}
	
		function _initData()
	{
		
    // Lets load the content if it doesn't already exist
		if (empty($this->_data) && $this->_id == '')
		{
			$item = new stdClass();
			$item->id					= 0;
			$item->title					= '';
			$item->meno				= '';
      $item->psc				= '';
			$item->stat				= $this->nastavenie->stat;
			$item->created_by_alias				= '';
			$item->alias				= '';
			$item->ico				= '';
			$item->dic				= '';
      $item->ic_dic				= '';
      $item->ordering				= '';
      $item->description				= '';
			$item->typ				= '';
			$item->or_sud				= '';
      $item->or_odd				= '';
      $item->or_vlozka			= '';
      $item->logo				= '';
      $item->ulica			= '';
      $item->mesto				= '';
      $item->published				= '1';
      
			$this->_data				= $item;
      return (boolean) $this->_data;
		}
	 	//**** zachovanie vyplnenych poli pri zlom zapise; zatial nefunguje ;(
    if (empty($this->_data) && $this->_id == 0)
		{
			$post = JRequest::get( 'get' );
			
      $item = new stdClass();
			$item->id					= 0;
			$item->title					= $post['title'];
			$item->meno				= $post['meno'];
      $item->ulica			= $post['ulica'];
      $item->mesto				= $post['mesto'];      
      $item->psc				= $post['psc'];
			$item->stat				= $post['stat'];
			$item->created_by_alias				= $post['created_by_alias'];
			$item->alias				= $post['alias'];
			$item->ico				= $post['ico'];
			$item->dic				= $post['dic'];
      $item->ic_dic				= $post['ic_dic'];
      $item->description				= $post['description'];
			$item->published				= $post['published'];
			$item->ordering				= $post['ordering'];
      $item->typ				= $post['typ'];
			$item->or_sud				= $post['or_sud'];
      $item->or_odd				= $post['or_odd'];
      $item->or_vlozka			= $post['or_vlozka'];
      $item->logo				= $post['logo'];
			
      $this->_data				= $item;
			return (boolean) $this->_data;
		}
 	return true;
	}

	function store($data)
	{
		global $mainframe;
		$user		= & JFactory::getUser();
		$row =& JTable::getInstance('upravapartner', '');
		// Bind the form fields to the table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
    
		// Make sure the data is valid
		if (!$row->check($elsettings)) {
			$this->setError($row->getError());
			return false;
		}

    // triedenie
    if (!$row->id) {
		  $row->ordering = $row->getNextOrder();
    }
    
		// Store the table to the database
		if (!$row->store(true)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return $row->id;
	}
}
?>